/******************************************************************************
 * Copyright (C) 671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#include "NodeCpp/ByteBuffer.h"
#include <sstream>
#include "NodeCpp/String.h"

namespace NodeCpp
{
	void ByteBufferException::_PrintPosError(void)
	{
		NodeCpp::snprintf(What_, WHAT_SIZE,
			"Attempted to %s in ByteBuffer (pos: %d size: %d ) "
			" value with size:  %d\n",
			(Add_ ? "put" : "get"), Pos_, Size_, OpSize_);
	}

    std::string NodeCpp::ByteBuffer::Dump(void)
    {
        std::stringstream _Ss;
        char _Buf[16];
        if (size() > 0)
        {
            const std::uint8_t *_BytePtr = contents();
            std::size_t _P = 0;
            while (true)
            {
                for (size_t _J = 0; _J < 16 && _P < size(); ++_J) {
                    NodeCpp::snprintf(_Buf, sizeof(_Buf), "%.2X ", _BytePtr[_P++]);
                    _Ss << _Buf;
                }

                if (_P < size()) {
                    _Ss << "\n";
                    continue;
                }
                break;
            }
        }
        _Ss << "\n";
        return _Ss.str();
    }
}
